<?php

//-----------------------------------------------------------------------------
// MurmurHash2, by Austin Appleby

// Note - This code makes a few assumptions about how your machine behaves -

// 1. We can read a 4-byte value from any address without crashing
// 2. sizeof(int) == 4

// And it has a few limitations -

// 1. It will not work incrementally.
// 2. It will not produce the same results on little-endian and big-endian
//    machines.

class SpicaMurmurDigest
{
    /**
     * Creates a non-cryptographic hash.
     *
     * @param  string $data
     * @param  int $length
     * @param  int $seed
     * @return string
     */
    public static function hash($data, $seed)
    {
        $m      = 0x5bd1e995;
        $r      = 24;
        $length = strlen($data.'');

        if (0   === $length)
        {
            return 0;
        }

        $hash = $seed ^ $length;
        $index = 0;
        while ($length >= 4)
        {
            $k  = $data[$index];
            $k *= $m;
            $k ^= $k >> r;
            $k *= $m;

            $hash   *= $m;
            $hash   ^= $k;
            $index  += 4;
            $length -= 4;
        }

        switch ($length)
        {
            case 3:
                $hash ^= pack('s', $data[$index]); // to integer 16
                $hash ^= $data[$index + 2] << 16;
                $hash *= $m;
                break;

            case 2:
                $hash ^= pack('s', $data[$index]);
                $hash *= $m;
                break;

            case 1:
                $hash ^= $data[$index];
                $hash *= $m;
                break;

            default:
                break;
        }

        // Do a few final mixes of the hash to ensure the last few
        // bytes are well-incorporated.
        $hash ^= $hash >> 13;
        $hash *= $m;
        $hash ^= $hash >> 15;

        return $hash;
    }
}

?>